package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.Role;

import sjtu.ist.warehouse4ws.service.PrivilegeService;
import sjtu.ist.warehouse4ws.service.RoleService;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShowRoleListAction extends ActionSupport{
	private RoleService roleService;
	private PrivilegeService privilegeService;
	private List<Role> roleList = new ArrayList<Role>();
	
	//获得角色列表
	public String showRoleList(){
		List<Role> templist=roleService.getRoleList();
		this.roleList = new ArrayList<Role>();
		
		ActionContext actionContext=ActionContext.getContext();
		Map session=actionContext.getSession();		
		int userType=(Integer)(session.get("user_type"));
		String userCompany=session.get("user_company").toString();		
		
		for(Role f : templist){
			if(privilegeService.checkPrivilege(f.getFileId(),userCompany,userType, "read")){
				roleList.add(f);
			}
		}
		return SUCCESS;
	}
	
	@JSON(name = "totalRole")
	public int getTotalRole() {
		return this.roleList.size();
	}

	@JSON(name = "Role")
	public List<Role> getRoleList() {
		return this.roleList;
	}
	
	// 注入DepartmentService
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}
	
	public void setPrivilegeService(PrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}

}
